<?php

class GiangvienController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update','upload'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete','upload','nhapgiangvien'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	 
	 
	 public function actionNhapgiangvien()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('nhapgiangvien');
	}
	 
		
	public function actionView($id)
	{
		$connection=Yii::app()->db;   // assuming you have configured a "db" connection
		$commandNgach=$connection->createCommand()
		->select('mangach,tenngach')
		->from('ngach')
		->query();
		$commandDonVi=$connection->createCommand()
		->select('madonvi,tendonvi')
		->from('donvicongtac')
		->query();
		
		$model=$this->loadModel($id);
		foreach($commandNgach as $row)
		{
			if($model->mangach==$row['mangach'])
				$model->mangach=$row['tenngach'];
		}
		//array('Cử nhân','Thạc sĩ','Tiến sĩ','Phó Giáo sư','Giáo sư')
		if($model->chucdanhhocvi==0)	$model->chucdanhhocvi='Cử nhân';
		else if($model->chucdanhhocvi==1) $model->chucdanhhocvi='Thạc sĩ';
		else if($model->chucdanhhocvi==2) $model->chucdanhhocvi='Tiến sĩ';
		else if($model->chucdanhhocvi==3) $model->chucdanhhocvi='Phó giáo sư';
		else $model->chucdanhhocvi='Giáo sư';
		if($model->isMale==1) $model->isMale='Nam';
		else $model->isMale='Nữ';
		$donvi= array();
		foreach($commandDonVi as $row)
		{
			$donvi[$row['madonvi']]=$row['tendonvi'];
			if($model->madonvi==$row['madonvi'])
				$model->madonvi=$row['tendonvi'];
		}
		//array('gvch'=>'Giảng viên cơ hữu','gvmg'=>'Giảng viên mời giảng','gvtgtt'=>'Giảng viên thỉnh giảng trong trường','gvtgnt'=>'Giảng viên thỉnh giảng ngoài trường')
		if($model->loaihinh=='gvch')	$model->loaihinh='Giảng viên cơ hữu';
		else if($model->loaihinh=='gvmg') $model->loaihinh='Giảng viên mời giảng';
		else if($model->loaihinh=='gvtgtt') $model->loaihinh='Giảng viên thỉnh giảng trong trường';
		else $model->loaihinh='Giảng viên thỉnh giảng ngoài trường';
		if($model->khoagiangday!=null)
		{
			$strkhoa='';
			if(is_array($model->khoagiangday))
			{
				$khoa=$model->khoagiangday;
				
				foreach($khoa as $rowkk=>$valuek)
				{
					foreach($donvi as $row=>$value)
					{
						if($valuek==$row)
						{
							$strkhoa.=$value.'; ';
						}
					}
				}
				$model->khoagiangday=$strkhoa;
			}
			else{
				foreach($donvi as $row=>$value)
				{
					if($model->khoagiangday==$row)
					{
						$strkhoa.=$value;
					}
				}
				$model->khoagiangday=$strkhoa;
			}
			
		}
		$this->render('view',array(
			'model'=>$model,
		));
	}

	/**
	 * Creates a new model.
	 * If creation is successful, the browser will be redirected to the 'view' page.
	 */
	public function actionCreate()
	{
		$model=new Giangvien;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Giangvien']))
		{
			$model->attributes=$_POST['Giangvien'];
			$khoagiangday=$_POST['Giangvien']['khoagiangday'];
			$strkhoagiangday=serialize($khoagiangday);
			$model->khoagiangday=$strkhoagiangday;
			//$model->ngaycap=substr($model->ngaycap,0,4).substr($model->ngaycap,5,2).substr($model->ngaycap,8,2);
			if($model->save())
				$this->redirect(array('view','id'=>$model->magv));
		}

		$this->render('create',array(
			'model'=>$model,
		));
	}

	/**
	 * Updates a particular model.
	 * If update is successful, the browser will be redirected to the 'view' page.
	 * @param integer $id the ID of the model to be updated
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST['Giangvien']))
		{
			$model->attributes=$_POST['Giangvien'];
			$khoagiangday=$_POST['Giangvien']['khoacothegiangday'];
			$strkhoagiangday=serialize($khoagiangday);
			$model->khoacothegiangday=$strkhoagiangday;
			if($model->save())
				$this->redirect(array('view','id'=>$model->magv));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Giangvien');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Giangvien('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Giangvien']))
		{
			$model->attributes=$_GET['Giangvien'];
			if($_GET['Giangvien']['mangach']=='rong')
				$model->mangach=null;
			if($_GET['Giangvien']['chucdanhhocvi']=='rong')
				$model->chucdanhhocvi=null;
			if($_GET['Giangvien']['madonvi']=='rong')
				$model->madonvi=null;
		}

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Giangvien the loaded model
	 * @throws CHttpException
	 */
	 
	public function loadModel($id)
	{
		function is_serialized( $data ) {
			// if it isn't a string, it isn't serialized
			if ( !is_string( $data ) )
				return false;
			$data = trim( $data );
			if ( 'N;' == $data )
				return true;
			if ( !preg_match( '/^([adObis]):/', $data, $badions ) )
				return false;
			switch ( $badions[1] ) {
				case 'a' :
				case 'O' :
				case 's' :
					if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
						return true;
					break;
				case 'b' :
				case 'i' :
				case 'd' :
					if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
						return true;
					break;
			}
			return false;
		}
		$model=Giangvien::model()->findByPk($id);
		
		if(is_serialized($model->khoagiangday))
		{
			$model->khoagiangday=unserialize($model->khoagiangday);
		}
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	
	/**
	 * Performs the AJAX validation.
	 * @param Giangvien $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='giangvien-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	

}
